/*************************************
 * Created by WebStorm
 * Author : 菲尔敦
 * QQ邮箱：2891044172@qq.com  手机号：15981761402
 * Created Time :2024/2/6 13:34
 Desc :
 **************************************/

import {ComputedRef, ref, computed,unref} from 'vue';
import {PaginationProps} from '../types/Pagination.ts'
import {isBoolean} from '@/util/is.ts';
import {paginationSetting} from '@/config';
import {DataTableProps} from 'naive-ui';
export function usePagination(props: ComputedRef<DataTableProps>) {
    const configRef = ref<DataTableProps>({});
    const getPaginationInfo = computed(() => {
        // @ts-ignore
        const {pagination} = props;
        if (isBoolean(pagination) && !pagination){
            return false;
        }
        const { totalField, itemCount } = paginationSetting;
        return {
            page:1,
            showSizePicker: true,
            showQuickJumper: true,
            itemCount: 0,
            pageSize:pagination?.pageSize || paginationSetting.defaultPageSize,
            pageSizes:pagination?.pageSizes || paginationSetting.pageSizes,
            ...(isBoolean(pagination) ? {} : pagination),
            ...unref(configRef),
            pageCount: unref(configRef)[totalField],
            prefix(config:any) {
                const total = config[itemCount];
                return '共 ' + total + ' 条数据';
            },
        }
    })
    const setPagination = (info: Partial<PaginationProps>) => {
        const paginationInfo = unref(getPaginationInfo);
        configRef.value = {
            ...(!isBoolean(paginationInfo) ? paginationInfo : {}),
            ...info,
        };
    }
    return {
        getPaginationInfo,
        setPagination
    }
}